<?php
include_once("./c/config.php");
require_once "jssdk.php";
function https_request($url, $type="get", $res="json", $data = ''){
    //1.初始化curl
    $curl = curl_init();
    //2.设置curl的参数
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SAFE_UPLOAD, true);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    if ($type == "post"){
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }
    //3.采集
    $output = curl_exec($curl);
    //var_dump(curl_getinfo($curl));
    //4.关闭
    curl_close($curl);
    if ($res == 'json') {
        return json_decode($output,true);
    }
}

$openid='';
$mysqli = new mysqli($dbhost, $dbuser, $dbpwd, $dbName);
if (!$mysqli->set_charset("utf8")) {printf("Error loading character set utf8: %s\n", $mysqliTwo->error);}

$query=$mysqli->query("select openid from ucard where code='1'  limit 0, 1");
if($query->num_rows>0){
     $getCard=$query->fetch_array();
     $openid=$getCard['openid'];
    
}
if (empty($openid)){
    echo 'openid empty';
    exit;
}
echo $openid;
echo "<br />";


$jssdk = new JSSDK($appId, $secret);
$acToken=$jssdk->getAccessToken();
$url='https://api.weixin.qq.com/card/user/getcardlist?access_token='.$acToken;

$data='{
  "openid": "'.$openid.'",
  "card_id": "'.$cardId.'"
}';
//echo $data;
echo "<br />";
$res = https_request( $url ,'post', 'json', $data);
if ($res['errmsg'] == 'ok'){
  $code=$res['card_list'][0]['code'];
  echo $code;
  echo "<br /><br />";
  $sql="update ucard set code='".$code."' where openid='".$openid."'  ";
  $mysqli->query($sql);
  echo $sql;
  echo "<br />";
}

echo "<br /><br />";
var_dump($res);

    
     

